/**
 * Created by qunxing.du on 2016/7/28.
 * Copyright 2016 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
package com.hw.one.collect.zzcity.handler;

import java.util.ArrayList;
import java.util.List;

/**
 * 郑州市数据处理器链
 */
public class HandlerChain implements IHandler {
    List<IHandler> handlers = new ArrayList<>();
    int index = -1;

    public HandlerChain add(IHandler handler) {
        handlers.add(handler);
        return this;
    }

    /**
     * 处理方法
     *
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public Object handle(Object o,String name, HandlerChain handlerChain) throws Exception {
        if (handlers.size() > index + 1) {
            IHandler handler = handlers.get(++index);
            if (handler != null) {
                return handler.handle(o, name,  handlerChain);
            }
        }
        return null;
    }
}
